package com.atgion.manager.service.impl;

import com.atgion.manager.mapper.CategoryMapper;
import com.atgion.manager.service.AdminCategoryService;
import com.atgion.spzx.model.bo.blog.CategoryDO;
import com.atgion.spzx.model.vo.base.PageResponse;
import com.atgion.spzx.model.vo.base.Result;
import com.atgion.spzx.model.vo.blog.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: 犬小哈
 * @url: www.quanxiaoha.com
 * @date: 2023-04-17 12:08
 * @description: TODO
 **/
@Service
@Slf4j
public class AdminCategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryDO> implements AdminCategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result addCategory(AddCategoryReqVO addCategoryReqVO) {
        String categoryName = addCategoryReqVO.getName();
        try {
            CategoryDO categoryDO = CategoryDO.builder()
                    .name(categoryName.trim())
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();
            save(categoryDO);
            return Result.success();
        } catch (DuplicateKeyException e) {
            log.error("==> 该分类已经存在：{}", categoryName, e);
            return Result.failed("该分类已经存在");
        }

    }

    @Override
    public Result<Page<CategoryDO>> queryCategoryPageList(QueryCategoryPageListReqVO queryCategoryPageListReqVO) {
        Long current = queryCategoryPageListReqVO.getCurrent();
        Long size = queryCategoryPageListReqVO.getSize();
        Page<CategoryDO> page = new Page<>(current, size);
        QueryWrapper<CategoryDO> wrapper = new QueryWrapper<>();

        String categoryName = queryCategoryPageListReqVO.getCategoryName();
        LocalDateTime startDate = queryCategoryPageListReqVO.getStartDate();
        LocalDateTime endDate = queryCategoryPageListReqVO.getEndDate();

        wrapper.lambda()
                .like(Objects.nonNull(categoryName), CategoryDO::getName, categoryName)
                .ge(Objects.nonNull(startDate), CategoryDO::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), CategoryDO::getCreateTime, endDate)
                .orderByDesc(CategoryDO::getCreateTime);

        Page<CategoryDO> categoryDOPage = page(page, wrapper);

        return Result.success(categoryDOPage);
    }

    @Override
    public Result deleteCategory(DeleteCategoryReqVO deleteCategoryReqVO) {
        Long categoryId = deleteCategoryReqVO.getCategoryId();
        removeById(categoryId);
        return Result.success();
    }

    @Override
    public Result queryCategorySelectList() {
        List<CategoryDO> categoryDOList =categoryMapper.selectList(null);
        List<QuerySelectListRspVO> list = null;
        if (!CollectionUtils.isEmpty(categoryDOList)) {
            list = categoryDOList.stream()
                    .map(p -> QuerySelectListRspVO.builder()
                            .label(p.getName())
                            .value(p.getId())
                            .build())
                    .collect(Collectors.toList());
        }
        return Result.success(list);
    }
}
